<?php

function cache_check($file) {
  
  $cache_filename =  variable_get('cache') . strToHex($file);

  //check if file is cached
  if (file_exists($cache_filename)) {
	$cache_contents = file_get_contents($cache_filename);
    $book_info = explode('|+|',$cache_contents);
    $book_info['isbn'] = $book_info[0];
    $book_info['title'] = $book_info[1];
    $book_info['author'] = $book_info[2];
	return $book_info;
  }
  else {
    return false;
  }
}

function fetch_book($searchtype,$string) {

  if (variable_get('isbndb_key','') == '') {
  
    echo 'No ISBNdb API Key found. Check config.php';
    return(false);
  } 
  else {
  
   $requestAddress = 'http://isbndb.com/api/books.xml?access_key='
                        . variable_get('isbndb_key') . '&index1='
                        . $searchtype . '&value1='
                        . $string;

   $xml_str = file_get_contents($requestAddress,0);
 

   $s = objectsIntoArray(simplexml_load_string($xml_str));
   
   // array_dump($s,'searching');//debug
   
   if ($searchtype == 'isbn' || $s["BookList"]["@attributes"]["total_results"] == 1) {
      $book_details = array ('isbn'=>$s["BookList"]["BookData"]["@attributes"]["isbn13"], 
                            'title'=>$s["BookList"]["BookData"]["Title"],
                            'author'=>$s["BookList"]["BookData"]["AuthorsText"],);
      $book_details['result'] = 1;
      // echo 'single<br>';//debug
    }
    elseif ($s["BookList"]["@attributes"]["shown_results"] > 1) {
      foreach ($s["BookList"]["BookData"] as $book_result) {
        $book_details[] = array('isbn'=>$book_result["@attributes"]["isbn13"],'title'=>$book_result["Title"],'author'=>$book_result["AuthorsText"]);
        $book_details['result'] = $s["BookList"]["@attributes"]["shown_results"];
        // echo 'multi<br>';//debug
      }
    }
    else {
      $book_details['result'] = 0;
      // echo 'fail<br>';//debug 
    }
    return $book_details;
  }
}

function cache_book($file, $book_details) {

  $cache_filename = variable_get('cache') . strToHex($file);

  // echo $cache_filename; //debug

  $book_cache = fopen($cache_filename, 'w+') or die('Check that cache directory is writeable');

  $cache_contents = $book_details['isbn'] . '|+|' . $book_details['title'] . '|+|' . $book_details['author'];

  fwrite($book_cache, $cache_contents);

  fclose($book_cache);

}

function getBooks( $path = '.', $level = 0 ){

  $books_index = array();
  
  // Directories to ignore when listing output. Many hosts
  // will deny PHP access to the cgi-bin.
  $ignore = array( 'cgi-bin', '.', '..','_Admin','_Deleted_Files','_Tools' ); 
  

  $dh = @opendir( $path );
  
  // Loop through the directory  
  while( false !== ( $file = readdir( $dh ) ) ){
  
      // Check that this file is not to be ignored
      if( !in_array( $file, $ignore ) ){
    
          // Its a directory, so we need to keep reading down...  
          if( is_dir( "$path/$file" ) ){
              // Re-call this same function but on a new directory.
              // this is what makes function recursive.
              
			  $books_index[] = $path . "/" . $file;
              getBooks( "$path/$file", ($level+1) );


          } else {

            $books_index[] = $path . "/" . $file;  
            
          }
        
      }
    
  }
    
  closedir( $dh );
  sort ($books_index);
  return $books_index;

}


function getIndex( $path = '.', $level = 0 ){ 

  $library_index = array();

  // Directories to ignore when listing output. Many hosts 
  // will deny PHP access to the cgi-bin. 
  $ignore = array( 'cgi-bin', '.', '..','_Admin','_Deleted_Files','_Tools' ); 

  $dh = @opendir( $path ); 
  
  // Loop through the directory    
  while( false !== ( $file = readdir( $dh ) ) ){ 

    // Check that this file is not to be ignored 
    if( !in_array( $file, $ignore ) ){ 
      
      // Its a directory, so we need to keep reading down...          
      if( is_dir( "$path/$file" ) ){ 
    
        // Re-call this same function but on a new directory. 
        // this is what makes function recursive.   
		$library_index[] = $path.$file;
        getIndex( "$path/$file", ($level+1) ); 
                   
      }   
    } 
     
  } 
     
  closedir( $dh ); 
  sort ($library_index);
  return $library_index;
} 


function hexToStr($hex) {
  $string='';
  for ($i=0; $i < strlen($hex)-1; $i+=2)
  {
    $string .= chr(hexdec($hex[$i].$hex[$i+1]));
  }
  return $string;
}


function strToHex($string) {
  $hex='';
  for ($i=0; $i < strlen($string); $i++)
  {
    $hex .= dechex(ord($string[$i]));
  }
  return $hex;
}

function variable_set($name, $value) {
  global $conf;

  $conf[$name] = $value;
}

function variable_get($name, $default = NULL) {
  global $conf;

  return isset($conf[$name]) ? $conf[$name] : $default;
}

function array_dump($value, $text = 'Test dump') {
  echo '<br />';
  echo $text;
  echo '<pre>';
  print_r($value);
  echo '</pre><br />';
}

function objectsIntoArray($arrObjData, $arrSkipIndices = array()) {
  $arrData = array();

  // if input is object, convert into array
  if (is_object($arrObjData)) {
    $arrObjData = get_object_vars($arrObjData);
  }

  if (is_array($arrObjData)) {
    foreach ($arrObjData as $index => $value) {
      if (is_object($value) || is_array($value)) {
        $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
      }
      if (in_array($index, $arrSkipIndices)) {
        continue;
      }
      $arrData[$index] = $value;
    }
  }
  return $arrData;
}

?>